<template>
    <div class="dashboard-container fade-in">
        <div class="page-header">
            <div class="header-content">
                <div class="title-section">
                    <h2 class="page-title">
                        <el-icon class="title-icon">
                            <DataBoard />
                        </el-icon>
                        系统仪表板
                    </h2>
                    <p class="page-subtitle">欢迎使用比赛报名费用管理系统</p>
                </div>
                <div class="header-actions">
                    <el-button type="primary" @click="refreshData">
                        <el-icon><Refresh /></el-icon>
                        刷新数据
                    </el-button>
                </div>
            </div>
        </div>

        <!-- 统计卡片 -->
        <div class="stats-grid">
            <el-card class="stat-card total-entries" shadow="hover">
                <div class="stat-content">
                    <div class="stat-icon">
                        <el-icon><Document /></el-icon>
                    </div>
                    <div class="stat-info">
                        <div class="stat-number">{{ totalEntries }}</div>
                        <div class="stat-label">总报名记录</div>
                        <div class="stat-trend">
                            <el-icon class="trend-icon up"><ArrowUp /></el-icon>
                            <span class="trend-text">+{{ newEntriesCount }}</span>
                            <span class="trend-period">本月新增</span>
                        </div>
                    </div>
                </div>
            </el-card>

            <el-card class="stat-card total-income" shadow="hover">
                <div class="stat-content">
                    <div class="stat-icon">
                        <el-icon><Money /></el-icon>
                    </div>
                    <div class="stat-info">
                        <div class="stat-number">¥{{ totalIncome.toFixed(2) }}</div>
                        <div class="stat-label">总收入</div>
                        <div class="stat-trend">
                            <el-icon class="trend-icon up"><ArrowUp /></el-icon>
                            <span class="trend-text">+{{ incomeGrowth }}%</span>
                            <span class="trend-period">较上月</span>
                        </div>
                    </div>
                </div>
            </el-card>

            <el-card class="stat-card active-competitions" shadow="hover">
                <div class="stat-content">
                    <div class="stat-icon">
                        <el-icon><Trophy /></el-icon>
                    </div>
                    <div class="stat-info">
                        <div class="stat-number">{{ activeCompetitions }}</div>
                        <div class="stat-label">进行中比赛</div>
                        <div class="stat-trend">
                            <el-icon class="trend-icon up"><ArrowUp /></el-icon>
                            <span class="trend-text">+{{ newCompetitionsCount }}</span>
                            <span class="trend-period">新增项目</span>
                        </div>
                    </div>
                </div>
            </el-card>

            <el-card class="stat-card pending-payments" shadow="hover">
                <div class="stat-content">
                    <div class="stat-icon">
                        <el-icon><Clock /></el-icon>
                    </div>
                    <div class="stat-info">
                        <div class="stat-number">{{ pendingPayments }}</div>
                        <div class="stat-label">待缴费记录</div>
                        <div class="stat-trend">
                            <el-icon class="trend-icon down"><ArrowDown /></el-icon>
                            <span class="trend-text">-{{ resolvedPayments }}</span>
                            <span class="trend-period">已处理</span>
                        </div>
                    </div>
                </div>
            </el-card>
        </div>

        <!-- 主要内容区域 -->
        <div class="main-content">
            <el-row :gutter="24">
                <!-- 左侧内容 -->
                <el-col :xs="24" :lg="16">
                    <!-- 最近活动 -->
                    <el-card class="activity-card" shadow="hover">
                        <template #header>
                            <div class="card-header">
                                <span>最近活动</span>
                                <el-button type="text" @click="viewAllActivities">查看全部</el-button>
                            </div>
                        </template>
                        <div class="activity-list">
                            <div v-for="activity in recentActivities" :key="activity.id" class="activity-item">
                                <div class="activity-icon" :class="activity.type">
                                    <el-icon>
                                        <component :is="activity.icon" />
                                    </el-icon>
                                </div>
                                <div class="activity-content">
                                    <div class="activity-title">{{ activity.title }}</div>
                                    <div class="activity-desc">{{ activity.description }}</div>
                                    <div class="activity-time">{{ formatTime(activity.time) }}</div>
                                </div>
                            </div>
                        </div>
                    </el-card>

                    <!-- 快速操作 -->
                    <el-card class="quick-actions-card" shadow="hover">
                        <template #header>
                            <div class="card-header">
                                <span>快速操作</span>
                            </div>
                        </template>
                        <div class="quick-actions-grid">
                            <div class="action-item" @click="navigateTo('/entries')">
                                <div class="action-icon">
                                    <el-icon><Document /></el-icon>
                                </div>
                                <div class="action-text">添加报名记录</div>
                            </div>
                            <div class="action-item" @click="navigateTo('/competitions')">
                                <div class="action-icon">
                                    <el-icon><Trophy /></el-icon>
                                </div>
                                <div class="action-text">管理比赛项目</div>
                            </div>
                            <div class="action-item" @click="navigateTo('/notifications')">
                                <div class="action-icon">
                                    <el-icon><Bell /></el-icon>
                                </div>
                                <div class="action-text">发布公告</div>
                            </div>
                            <div class="action-item" @click="navigateTo('/statistics')">
                                <div class="action-icon">
                                    <el-icon><Money /></el-icon>
                                </div>
                                <div class="action-text">查看统计</div>
                            </div>
                        </div>
                    </el-card>
                </el-col>

                <!-- 右侧内容 -->
                <el-col :xs="24" :lg="8">
                    <!-- 系统状态 -->
                    <el-card class="system-status-card" shadow="hover">
                        <template #header>
                            <div class="card-header">
                                <span>系统状态</span>
                                <el-tag type="success" size="small">运行正常</el-tag>
                            </div>
                        </template>
                        <div class="status-list">
                            <div class="status-item">
                                <span class="status-label">系统版本</span>
                                <span class="status-value">v1.0.0</span>
                            </div>
                            <div class="status-item">
                                <span class="status-label">在线用户</span>
                                <span class="status-value">{{ onlineUsers }}</span>
                            </div>
                            <div class="status-item">
                                <span class="status-label">今日访问</span>
                                <span class="status-value">{{ todayVisits }}</span>
                            </div>
                            <div class="status-item">
                                <span class="status-label">数据更新时间</span>
                                <span class="status-value">{{ lastUpdateTime }}</span>
                            </div>
                        </div>
                    </el-card>

                    <!-- 通知公告 -->
                    <el-card class="notifications-card" shadow="hover">
                        <template #header>
                            <div class="card-header">
                                <span>最新公告</span>
                                <el-button type="text" @click="navigateTo('/notifications')">更多</el-button>
                            </div>
                        </template>
                        <div class="notification-list">
                            <div v-for="notification in recentNotifications" :key="notification.id" class="notification-item">
                                <div class="notification-header">
                                    <el-tag :type="getNotificationTypeColor(notification.type)" size="small">
                                        {{ notification.type }}
                                    </el-tag>
                                    <span class="notification-time">{{ formatTime(notification.time) }}</span>
                                </div>
                                <div class="notification-title">{{ notification.title }}</div>
                                <div class="notification-content">{{ notification.content }}</div>
                            </div>
                        </div>
                    </el-card>

                    <!-- 快捷链接 -->
                    <el-card class="quick-links-card" shadow="hover">
                        <template #header>
                            <div class="card-header">
                                <span>快捷链接</span>
                            </div>
                        </template>
                        <div class="links-list">
                            <div class="link-item" @click="navigateTo('/settings')">
                                <el-icon><Setting /></el-icon>
                                <span>系统设置</span>
                            </div>
                            <div class="link-item" @click="navigateTo('/profile')">
                                <el-icon><User /></el-icon>
                                <span>个人信息</span>
                            </div>
                            <div class="link-item" @click="exportData">
                                <el-icon><Download /></el-icon>
                                <span>数据导出</span>
                            </div>
                            <div class="link-item" @click="showHelp">
                                <el-icon><QuestionFilled /></el-icon>
                                <span>帮助文档</span>
                            </div>
                        </div>
                    </el-card>
                </el-col>
            </el-row>
        </div>
    </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { 
    DataBoard, Refresh, Document, Money, Trophy, Clock, 
    ArrowUp, ArrowDown, Bell, Setting, User, Download, 
    QuestionFilled, Plus, Edit, CircleCheck 
} from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { useEntriesStore } from '../stores/entries'

const router = useRouter()
const entriesStore = useEntriesStore()

// 统计数据
const totalEntries = ref(156)
const totalIncome = ref(45678.50)
const activeCompetitions = ref(8)
const pendingPayments = ref(23)
const newEntriesCount = ref(12)
const incomeGrowth = ref(8.5)
const newCompetitionsCount = ref(2)
const resolvedPayments = ref(5)
const onlineUsers = ref(15)
const todayVisits = ref(234)
const lastUpdateTime = ref('刚刚')

// 最近活动
const recentActivities = ref([
    {
        id: 1,
        type: 'success',
        icon: 'Plus',
        title: '新增报名记录',
        description: '张三报名了蓝桥杯程序设计大赛',
        time: new Date(Date.now() - 5 * 60 * 1000)
    },
    {
        id: 2,
        type: 'primary',
        icon: 'Money',
        title: '缴费完成',
        description: '李四完成了传智杯算法竞赛的缴费',
        time: new Date(Date.now() - 15 * 60 * 1000)
    },
    {
        id: 3,
        type: 'warning',
        icon: 'Bell',
        title: '发布公告',
        description: '系统管理员发布了新的比赛通知',
        time: new Date(Date.now() - 30 * 60 * 1000)
    },
    {
        id: 4,
        type: 'info',
        icon: 'Trophy',
        title: '新增比赛项目',
        description: '添加了2024年数学建模竞赛',
        time: new Date(Date.now() - 60 * 60 * 1000)
    }
])

// 最新公告
const recentNotifications = ref([
    {
        id: 1,
        type: '重要公告',
        title: '关于2024年蓝桥杯程序设计大赛报名的通知',
        content: '2024年蓝桥杯程序设计大赛报名工作即将开始...',
        time: new Date(Date.now() - 2 * 60 * 60 * 1000)
    },
    {
        id: 2,
        type: '一般通知',
        title: '传智杯算法竞赛获奖名单公示',
        content: '经过激烈角逐，传智杯算法竞赛获奖名单已确定...',
        time: new Date(Date.now() - 4 * 60 * 60 * 1000)
    },
    {
        id: 3,
        type: '活动通知',
        title: '2024年春季学期竞赛活动安排',
        content: '2024年春季学期将举办多项学科竞赛活动...',
        time: new Date(Date.now() - 6 * 60 * 60 * 1000)
    }
])

// 格式化时间
const formatTime = (time) => {
    const now = new Date()
    const diff = now - time
    
    if (diff < 60 * 1000) {
        return '刚刚'
    } else if (diff < 60 * 60 * 1000) {
        return `${Math.floor(diff / (60 * 1000))}分钟前`
    } else if (diff < 24 * 60 * 60 * 1000) {
        return `${Math.floor(diff / (60 * 60 * 1000))}小时前`
    } else {
        return time.toLocaleDateString('zh-CN')
    }
}

// 获取通知类型颜色
const getNotificationTypeColor = (type) => {
    const colors = {
        '重要公告': 'danger',
        '一般通知': 'primary',
        '活动通知': 'success'
    }
    return colors[type] || 'info'
}

// 刷新数据
const refreshData = () => {
    ElMessage.success('数据已刷新')
    // 这里可以添加实际的数据刷新逻辑
}

// 导航到指定页面
const navigateTo = (path) => {
    router.push(path)
}

// 查看所有活动
const viewAllActivities = () => {
    ElMessage.info('查看所有活动')
}

// 导出数据
const exportData = () => {
    ElMessage.info('导出数据功能')
}

// 显示帮助
const showHelp = () => {
    ElMessage.info('帮助文档')
}

onMounted(() => {
    // 初始化数据
    totalEntries.value = entriesStore.entries.length
    totalIncome.value = entriesStore.entries.reduce((sum, entry) => sum + entry.fee, 0)
})
</script>

<style scoped>
.dashboard-container {
    animation: fadeIn 0.5s ease-out;
}

.page-header {
    margin-bottom: var(--spacing-xl);
}

.header-content {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: var(--spacing-lg);
}

.title-section {
    flex: 1;
}

.page-title {
    display: flex;
    align-items: center;
    gap: var(--spacing-md);
    margin: 0 0 var(--spacing-sm) 0;
    font-size: 1.75rem;
    font-weight: 700;
    color: var(--text-primary);
}

.title-icon {
    font-size: 1.5rem;
    color: var(--primary-color);
}

.page-subtitle {
    margin: 0;
    color: var(--text-secondary);
    font-size: 0.9rem;
}

/* 统计卡片 */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: var(--spacing-lg);
    margin-bottom: var(--spacing-xl);
}

.stat-card {
    border-radius: var(--radius-lg);
    transition: all 0.3s ease;
    cursor: pointer;
}

.stat-card:hover {
    transform: translateY(-4px);
    box-shadow: var(--shadow-xl);
}

.stat-content {
    display: flex;
    align-items: center;
    gap: var(--spacing-lg);
    padding: var(--spacing-md);
}

.stat-icon {
    width: 60px;
    height: 60px;
    border-radius: var(--radius-lg);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
    color: white;
}

.total-entries .stat-icon {
    background: linear-gradient(135deg, var(--primary-color), var(--primary-light));
}

.total-income .stat-icon {
    background: linear-gradient(135deg, var(--success-color), #34d399);
}

.active-competitions .stat-icon {
    background: linear-gradient(135deg, var(--warning-color), #fbbf24);
}

.pending-payments .stat-icon {
    background: linear-gradient(135deg, var(--danger-color), #f87171);
}

.stat-info {
    flex: 1;
}

.stat-number {
    font-size: 1.75rem;
    font-weight: 700;
    color: var(--text-primary);
    line-height: 1;
}

.stat-label {
    font-size: 0.875rem;
    color: var(--text-secondary);
    margin-top: var(--spacing-xs);
}

.stat-trend {
    display: flex;
    align-items: center;
    gap: var(--spacing-xs);
    margin-top: var(--spacing-sm);
}

.trend-icon {
    font-size: 0.875rem;
}

.trend-icon.up {
    color: var(--success-color);
}

.trend-icon.down {
    color: var(--danger-color);
}

.trend-text {
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--text-primary);
}

.trend-period {
    font-size: 0.75rem;
    color: var(--text-secondary);
}

/* 主要内容区域 */
.main-content {
    margin-bottom: var(--spacing-xl);
}

.card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-weight: 600;
    color: var(--text-primary);
}

/* 活动列表 */
.activity-card {
    margin-bottom: var(--spacing-lg);
    border-radius: var(--radius-lg);
}

.activity-list {
    max-height: 400px;
    overflow-y: auto;
}

.activity-item {
    display: flex;
    align-items: flex-start;
    gap: var(--spacing-md);
    padding: var(--spacing-md) 0;
    border-bottom: 1px solid var(--border-color);
}

.activity-item:last-child {
    border-bottom: none;
}

.activity-icon {
    width: 40px;
    height: 40px;
    border-radius: var(--radius-md);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1rem;
    color: white;
    flex-shrink: 0;
}

.activity-icon.success {
    background: var(--success-color);
}

.activity-icon.primary {
    background: var(--primary-color);
}

.activity-icon.warning {
    background: var(--warning-color);
}

.activity-icon.info {
    background: var(--info-color);
}

.activity-content {
    flex: 1;
}

.activity-title {
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: var(--spacing-xs);
}

.activity-desc {
    font-size: 0.875rem;
    color: var(--text-secondary);
    margin-bottom: var(--spacing-xs);
}

.activity-time {
    font-size: 0.75rem;
    color: var(--text-secondary);
}

/* 快速操作 */
.quick-actions-card {
    margin-bottom: var(--spacing-lg);
    border-radius: var(--radius-lg);
}

.quick-actions-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
    gap: var(--spacing-md);
}

.action-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--spacing-sm);
    padding: var(--spacing-lg);
    border-radius: var(--radius-md);
    background: var(--bg-secondary);
    transition: all 0.3s ease;
    cursor: pointer;
}

.action-item:hover {
    background: var(--primary-color);
    color: white;
    transform: translateY(-2px);
    box-shadow: var(--shadow-md);
}

.action-icon {
    width: 48px;
    height: 48px;
    border-radius: var(--radius-lg);
    background: var(--primary-color);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.25rem;
    transition: all 0.3s ease;
}

.action-item:hover .action-icon {
    background: white;
    color: var(--primary-color);
}

.action-text {
    font-size: 0.875rem;
    font-weight: 500;
    text-align: center;
}

/* 系统状态 */
.system-status-card {
    margin-bottom: var(--spacing-lg);
    border-radius: var(--radius-lg);
}

.status-list {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-md);
}

.status-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: var(--spacing-sm) 0;
    border-bottom: 1px solid var(--border-color);
}

.status-item:last-child {
    border-bottom: none;
}

.status-label {
    color: var(--text-secondary);
    font-size: 0.875rem;
}

.status-value {
    font-weight: 600;
    color: var(--text-primary);
}

/* 通知公告 */
.notifications-card {
    margin-bottom: var(--spacing-lg);
    border-radius: var(--radius-lg);
}

.notification-list {
    max-height: 300px;
    overflow-y: auto;
}

.notification-item {
    padding: var(--spacing-md) 0;
    border-bottom: 1px solid var(--border-color);
}

.notification-item:last-child {
    border-bottom: none;
}

.notification-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: var(--spacing-xs);
}

.notification-time {
    font-size: 0.75rem;
    color: var(--text-secondary);
}

.notification-title {
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: var(--spacing-xs);
    font-size: 0.875rem;
}

.notification-content {
    font-size: 0.8rem;
    color: var(--text-secondary);
    line-height: 1.4;
}

/* 快捷链接 */
.quick-links-card {
    border-radius: var(--radius-lg);
}

.links-list {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-sm);
}

.link-item {
    display: flex;
    align-items: center;
    gap: var(--spacing-sm);
    padding: var(--spacing-md);
    border-radius: var(--radius-md);
    background: var(--bg-secondary);
    transition: all 0.3s ease;
    cursor: pointer;
}

.link-item:hover {
    background: var(--primary-color);
    color: white;
    transform: translateX(4px);
}

.link-item .el-icon {
    font-size: 1.1rem;
}

/* 响应式设计 */
@media (max-width: 768px) {
    .header-content {
        flex-direction: column;
        align-items: stretch;
        gap: var(--spacing-md);
    }
    
    .stats-grid {
        grid-template-columns: 1fr;
        gap: var(--spacing-md);
    }
    
    .quick-actions-grid {
        grid-template-columns: repeat(2, 1fr);
    }
    
    .page-title {
        font-size: 1.5rem;
    }
    
    .stat-content {
        padding: var(--spacing-sm);
    }
    
    .stat-icon {
        width: 50px;
        height: 50px;
        font-size: 1.25rem;
    }
    
    .stat-number {
        font-size: 1.5rem;
    }
}
</style>
